package cloud.mbase.controller;

import cloud.mbase.business.interf.UserBusinessInterface;
import cloud.mbase.entity.Result;
import cloud.mbase.entity.datavo.Muser;
import cloud.mbase.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/base/user")
public class UserController {
    @Autowired
    private UserBusinessInterface userBusiness;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping(value = "/signup",method = RequestMethod.POST)
    public Result signup(@RequestBody Muser user){
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userBusiness.saveUser(user);
        return ResultUtil.getSuccessResult();
    }
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value = "/auth/{userId}/{role}",method = RequestMethod.POST)
    public Result setAuth(@PathVariable Integer userId,@PathVariable String role){
        boolean status = userBusiness.setUserAuth(userId,role);
        if(status){
            return ResultUtil.getSuccessResult();
        }
        return ResultUtil.getFailResult();
    }
}
